/* ------------------------------------------------------------------------*
 *
 * ------------------------------------------------------------------------*/
#define _TOUCH_MODULE_
#include "touch.h"
#include "gd32f1x0_conf.h"
#include "systimer.h"

/*****************************************************************************//*!
* @brief   	touch modle init .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void TouchInit(void)
{
	/* Configure the GPIO ports */
    GPIO_InitPara  GPIO_InitStructure;
	
	RCC_AHBPeriphClock_Enable(RCC_AHBPERIPH_TSI, ENABLE);
	
	/* Connect PXx to USARTx_Tx */
    GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE0, GPIO_AF_3);
    /* Connect PXx to USARTx_Rx */
    GPIO_PinAFConfig(GPIOB, GPIO_PINSOURCE1, GPIO_AF_3);

    /* Configure USART Rx/Tx as alternate function push-pull */
    GPIO_InitStructure.GPIO_Pin     = GPIO_PIN_0 | GPIO_PIN_1;
    GPIO_InitStructure.GPIO_Mode    = GPIO_MODE_AF;
    GPIO_InitStructure.GPIO_Speed   = GPIO_SPEED_50MHZ;
    GPIO_InitStructure.GPIO_OType   = GPIO_OTYPE_PP;
    GPIO_InitStructure.GPIO_PuPd    = GPIO_PUPD_NOPULL;
    GPIO_Init(GPIOB , &GPIO_InitStructure);
	
	TSI->CTLR = TSI_CTCDIV_1 |
				TSI_CTDT_2CTCLK |
				TSI_CDT_2CTCLK |
				TSI_SOFTWARE_TRIGGLER |
				TSI_RISING_EDGE_TRIGGLER |
				TSI_MCN_4095_CYCLE;
	//disable int
	TSI->IER = 0;
	//clear int flag
	TSI->CEFR = 0x03;
	//
	TSI->PHMR = 0;
	//switch control
	TSI->ASWR = 0;
	//enable group
	TSI->GCTLR = 0x04;
	//Sample Pin set
	TSI->SPR = TSI_GROUP3_PIN2;
	//Channel Pin set
	TSI->CPR = TSI_GROUP3_PIN3;
	
	TSI_Enable( ENABLE);
}

/*****************************************************************************//*!
* @brief   	touch modle check .
*               
* @param   none
*
* @return  none
*
* @ Pass/ Fail criteria: none
*****************************************************************************/

void TouchCheck(void)
{
	uint32_t ChCycle;
	
	if(bPulse_100ms)
	{
		//clear int flag
		TSI->CEFR = 0x03;
		TSI_StartAcquisition( ENABLE );
	}
	
	if(TSI->STR)
	{
		ChCycle = TSI->GXCYCNR[2];
		TSI->CEFR = 0x03;
		
		if(ChCycle < 0xC0)
		{
		
		}		
	}

}
